﻿using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;

namespace WikiUploader.Classes
{
   public static class Extensions
   {
      /// <summary>
      /// Converts a secure string to a regular string.
      /// </summary>
      /// <param name="securePassword">Secure string.</param>
      /// <returns>Regular string.</returns>
      public static string ConvertToUnsecureString (this SecureString securePassword)
      {
         if (securePassword == null)
            throw new ArgumentNullException ("securePassword");

         IntPtr unmanagedString = IntPtr.Zero;
         try
         {
            unmanagedString = Marshal.SecureStringToGlobalAllocUnicode (securePassword);
            return Marshal.PtrToStringUni (unmanagedString);
         }
         finally
         {
            Marshal.ZeroFreeGlobalAllocUnicode (unmanagedString);
         }
      }
   }
}
